﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Subsonic.Data;
using MichMan.Utilities.Requests;

namespace Subsonic.ViewModels
{
    public class ArtistAlbumItemsViewModel : PivotItemViewModel
    {
        public ArtistAlbumItemsViewModel(Artist artist)
        {
            Artist = artist;
            Id = artist.Id;
            Title = artist.Name;
        }

        public Artist Artist { get; private set; }

        public override IRequest LoadItems()
        {
            GetMusicDirectoryRequest getMusicDirectoryRequest = new GetMusicDirectoryRequest(Id);

            getMusicDirectoryRequest.RunRequestCompleted += new MichMan.Utilities.Requests.RunRequestCompletedEventHandler(musicDirReq_RunRequestCompleted);
            getMusicDirectoryRequest.RunRequestAsync();
            return getMusicDirectoryRequest;
        }

        void musicDirReq_RunRequestCompleted(object sender, MichMan.Utilities.Requests.RunRequestCompletedEventArgs e)
        {
            GetMusicDirectoryRequest getMusicDirectoryRequest = (GetMusicDirectoryRequest)sender;

            if (getMusicDirectoryRequest.Response == null || getMusicDirectoryRequest.RequestStatus != RequestStatus.Succeeded)
            {
                return;
            }

            foreach (var album in getMusicDirectoryRequest.Response.Directory.Children)
            {
                if (album.IsDir)
                {
                    Items.Add(new AlbumItem(Artist, album));
                }
            }
        }

    }
}
